﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;

public partial class VendorManagement : System.Web.UI.Page
{
    private static string strConn = ConfigurationManager.ConnectionStrings["LabConnectionString"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        populateData();
    }

    private void populateData()
    {
        DataTable dt = new DataTable();
        using (SqlDataAdapter da = new SqlDataAdapter("sp_list_vendor", strConn))
        {
            da.Fill(dt);
        }
        userList.DataSource = dt;
        userList.DataBind();
    }

    //[WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]
    //public static DatabaseResponse TambahVendor(string nama, string alamat, string kontak, string telepon, string keterangan, int user)
    //{
    //    DatabaseResponse result = new DatabaseResponse();
    //    try
    //    {
    //        using (SqlConnection cn = new SqlConnection(strConn))
    //        {
    //            cn.Open();
    //            string query = "sp_tambah_vendor '{0}', '{1}', '{2}', '{3}', '{4}'";
    //            using (SqlCommand cmd = new SqlCommand(string.Format(query, nama, alamat, kontak, telepon, keterangan), cn))
    //            {
    //                int ra = cmd.ExecuteNonQuery();
    //                if (ra > 0)
    //                {
    //                    result.Status = true;
    //                }
    //                else
    //                {
    //                    result.Status = false;
    //                    result.ErrorMessage = "Tidak ada data yang berhasil ditulis.";
    //                }
    //            }
    //            cn.Close();
    //        }
    //    }
    //    catch (Exception ex)
    //    {

    //        result.Status = false;
    //        result.ErrorMessage = ex.Message;
    //    }
    //    return result;
    //}

    //public class DatabaseResponse
    //{
    //    public bool Status;
    //    public string ErrorMessage;
    //    public string Value;
    //}

    private void tambahVendor(string nama, string alamat, string kontak, string telepon, string keterangan, int user)
    {
        using (SqlConnection cn = new SqlConnection(strConn))
        {
            cn.Open();
            string query = "sp_tambah_vendor '{0}', '{1}', '{2}', '{3}', '{4}'";
            using (SqlCommand cmd = new SqlCommand(string.Format(query, nama, alamat, kontak, telepon, keterangan), cn))

            //using (SqlCommand cmd = new SqlCommand("sp_tambah_lab '" + kemasan + "'", cn))
            {
                try
                {
                    int ra = cmd.ExecuteNonQuery();
                    if (ra > 0)
                    {
                        //this.namaLab.Value = string.Empty;
                        showMessage("Sukses Menambahkan Vendor", "Vendor <b>" + nama + "</b> berhasil ditambahkan.", "success");
                        populateData();
                    }
                    else
                    {
                        showMessage("Gagal Menambahkan Vendor", "Tidak ada data yang ditambahkan", "error");
                    }
                }
                catch (Exception ex)
                {
                    string msg = ex.Message;
                    if (msg.Contains("Cannot insert duplicate key"))
                    {
                        msg = "Vendor '" + nama + "' sudah pernah terdaftar.";
                    }
                    showMessage("Gagal Menambahkan Vendor", msg, "error");
                }
            }
        }
    }

    private void showMessage(string caption, string message, string type)
    {
        message = message.Replace("'", "&apos;");
        message = message.Replace(Environment.NewLine, "<br />");
        string clientScript = "$(document).ready(function(){ new PNotify({ title: '" + caption + "', text: '" + message + "', type: '" + type + "',styling: 'bootstrap3' }); });";
        ScriptManager.RegisterStartupScript(Page, this.GetType(), "ShowMessage", clientScript, true);
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        tambahVendor(nama.Value, alamat.Value, kontak.Value, telepon.Value, keterangan.Value, int.Parse(user.Value));
    }
}